package model;
import java.util.Iterator;
import java.util.List;
import java.util.Random;


public class Hunter implements Actor{

	
	 // Whether the hunter is alive or not.
    private boolean alive;
    // The hunter field.
    private Field field;
    // The hunter position in the field.
    private Location location;
    
    // Number of steps between targets
    private static int STEPS_BETWEEN_HUNT = 3;
    
    // Number of steps since last hunt
    private int stepsSinceLastHunt;
    
    /**
     * Create a new hunter at location in field.
     * 
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Hunter(Field field, Location location)
    {
        alive = true;
        this.field = field;
        setLocation(location);
        stepsSinceLastHunt = 3;
    }
    
	/**
     * Make this hunter act - that is: make it do
     * whatever it wants/needs to do.
     * @param newhunters A list to receive newly born hunters.
     */
    public void act(List<Actor> newActors) {
    	move();
    	stepsSinceLastHunt++;
	}

    private void move() {
    	Location newLocation = null;
        // Move towards a target if found.
    	if(stepsSinceLastHunt >= STEPS_BETWEEN_HUNT){
    		newLocation = findTarget();
    	}
        if(newLocation == null) { 
            // No target found - try to move to a free location.
            newLocation = getField().freeAdjacentLocation(getLocation());
        }
        // See if it was possible to move.
        if(newLocation != null) {
        	setLocation(newLocation);
        }
    }
    
    private Location findTarget() {
            Field field = getField();
            List<Location> adjacent = field.adjacentLocations(getLocation());
            Iterator<Location> it = adjacent.iterator();
            while(it.hasNext()) {
                Location where = it.next();
                Object target = field.getObjectAt(where);
                if(target instanceof Animal) {
                	Animal animal = (Animal) target;
                    if(animal.isAlive()) { 
                        animal.setDead();
                        System.out.println("Animal got shot, It ain't easy being wild");
                        stepsSinceLastHunt = 0;
                        return where;
                    }
                }
            }
            return null;
      }
   
    
    /**
     * Check whether the hunter is alive or not.
     * @return true if the hunter is still alive.
     */
    protected boolean isAlive()
    {
        return alive;
    }

    /**
     * Indicate that the hunter is no longer alive.
     * It is removed from the field.
     */
    protected void setDead()
    {
        alive = false;
        if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }

    /**
     * Return the hunter's location.
     * @return The hunter's location.
     */
    protected Location getLocation()
    {
        return location;
    }
    
    /**
     * Place the hunter at the new location in the given field.
     * @param newLocation The hunter's new location.
     */
    protected void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }
    
    public boolean isActive() {
    	return true;
    }
    
    /**
     * Return the hunter's field.
     * @return The hunter's field.
     */
    protected Field getField()
    {
        return field;
    }    
    
    public String toString() {
    	return "Hunter";
    }
}


